package com.lbmb.mdps.settings.controller;

import com.lbmb.mdps.commons.constant.Constant;
import com.lbmb.mdps.commons.domain.EmployeeReceive;
import com.lbmb.mdps.commons.domain.EmployeeVo;
import com.lbmb.mdps.commons.domain.ReturnObjectVO;
import com.lbmb.mdps.commons.domain.UserReceive;
import com.lbmb.mdps.commons.exception.EmployeeException;
import com.lbmb.mdps.commons.utils.DateTimeUtil;
import com.lbmb.mdps.commons.utils.Md5Util;
import com.lbmb.mdps.commons.utils.SnowFlakeIdUtil;
import com.lbmb.mdps.settings.domain.Employee;
import com.lbmb.mdps.settings.service.AdministratorService;
import com.lbmb.mdps.settings.service.EmployeeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 林文塨
 * @date 2022-05-30 8:58
 * description
 */
@CrossOrigin
@RestController
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;
    @Resource
    private AdministratorService administratorService;

    @PostMapping( "/settings/employee/login.do")
    public ReturnObjectVO login(@RequestBody Employee employee) throws EmployeeException {
        /*
         * 员工登录功能
         * @author 林文塨
         * @date 2022/5/30 9:02
         * @param [employee]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        if (employee.getPassword().length() != 32) {
            employee.setPassword(Md5Util.getMd5(employee.getPassword()));
        }

        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);
        rov.setOthers(employeeService.login(employee));

        return rov;
    }

    @PostMapping( "/settings/employee/employeePageList.do")
    public ReturnObjectVO employeePageList(@RequestBody EmployeeReceive employeeReceive) throws EmployeeException {
        /*
         * 分页获取员工表功能
         * @author 林文塨
         * @date 2022/5/30 10:16
         * @param [employee]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        employeeReceive.setSkipCount((employeeReceive.getPageNo()-1)*employeeReceive.getPageSize());

        List<Employee> employeeList = employeeService.employeePageList(employeeReceive);

        rov.setOthers(employeeList);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/settings/employee/getEmployeeTotal.do")
    public ReturnObjectVO getEmployeeTotal(@RequestBody EmployeeReceive employeeReceive) throws EmployeeException {
        /*
         * 获取员工分页总条数功能
         * @author 林文塨
         * @date 2022/5/30 10:38
         * @param [employeeReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        Integer total = employeeService.getEmployeeTotal(employeeReceive);
        rov.setNumber(total);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/settings/employee/addEmployee.do")
    public ReturnObjectVO addEmployee(@RequestBody Employee employee) throws EmployeeException {
        /*
         * 添加员工
         * @author 林文塨
         * @date 2022/5/30 12:38
         * @param [employeeReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        employee.setPassword(Md5Util.getMd5(employee.getPassword()));
        employee.setState("已启用");
        employee.setCreateTime(DateTimeUtil.getSysTime());
        employee.setCreateBy(employee.getCurrentUserId());
        employee.setDeleteFlag("0");
        employee.setId(SnowFlakeIdUtil.generateId());

        employeeService.addEmployee(employee);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/settings/employee/editEmployee.do")
    public ReturnObjectVO editEmployee(@RequestBody Employee employee) throws EmployeeException {
        /*
         * 修改员工
         * @author 林文塨
         * @date 2022/5/30 12:38
         * @param [employeeReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        if (employee.getPassword().length() != 32) {
            employee.setPassword(Md5Util.getMd5(employee.getPassword()));
        }

        employee.setEditTime(DateTimeUtil.getSysTime());
        employee.setEditBy(employee.getCurrentUserId());

        employeeService.editEmployee(employee);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/settings/employee/deleteEmployee.do")
    public ReturnObjectVO deleteEmployee(@RequestBody String [] deleteEmployeeIds) throws EmployeeException {
        /*
         * 删除员工
         * @author 林文塨
         * @date 2022/5/30 16:17
         * @param [employee]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        employeeService.deleteEmployee(deleteEmployeeIds);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/settings/employee/acceptEmployee.do")
    public ReturnObjectVO acceptEmployee(@RequestBody String [] editEmployeeStatusIds) throws EmployeeException {
        /*
         * 启用员工状态
         * @author 林文塨
         * @date 2022/5/30 16:45
         * @param [deleteEmployeeIds]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        employeeService.acceptEmployee(editEmployeeStatusIds);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/settings/employee/forbiddenEmployee.do")
    public ReturnObjectVO forbiddenEmployee(@RequestBody String [] editEmployeeStatusIds) throws EmployeeException {
        /*
         * 禁用员工状态
         * @author 林文塨
         * @date 2022/5/30 16:45
         * @param [deleteEmployeeIds]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        employeeService.forbiddenEmployee(editEmployeeStatusIds);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @GetMapping( "/settings/employee/getEmployeeData.do")
    public ReturnObjectVO getEmployeeData() {
        /*
         * 获取全部员工
         * @author 林文塨
         * @date 2022/5/30 16:17
         * @param [employee]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        List<EmployeeVo> employeeData = employeeService.getAllEmployee();

        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);
        rov.setOthers(employeeData);

        return rov;
    }

    @PostMapping( "/settings/employee/editPassword.do")
    public ReturnObjectVO editPassword(@RequestBody UserReceive userReceive) throws EmployeeException {
        /*
         * 修改员工密码
         * @author 林文塨
         * @date 2022/5/30 16:17
         * @param [employee]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        employeeService.editPassword(userReceive);

        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }
}
